import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import CustomSwing.JCLabel;
import CustomSwing.JCTextField;

@SuppressWarnings("serial")
public class VendorInfoPanel extends JPanel {
	public String USER_FUNC = "Update Vendor Info";
	private Font custFont = new Font("Arial Bold", Font.BOLD, 15);

	Db db;
	private JComboBox vidCB = new JComboBox();
	private ButtonGroup paidBG = new ButtonGroup();

	private String vID;
	private JCTextField fNameTF = new JCTextField("");
	private JCTextField lNameTF = new JCTextField("");
	private JCTextField addressTF = new JCTextField("");
	private JCTextField phoneTF = new JCTextField("");
	private JCTextField emailTF = new JCTextField("");
	private JCTextField ubiTF = new JCTextField("");
	private JCTextField bNameTF = new JCTextField("");
	private JCTextField stallNumTF = new JCTextField("");
	private JRadioButton yesRB = new JRadioButton(); 
	private JRadioButton noRB = new JRadioButton();

	public VendorInfoPanel(Db theDB) {
		db = theDB;
		setVIDComboBox();
		setRadioButton(yesRB, "YES");
		setRadioButton(noRB, "NO");

		JPanel formPanel = new JPanel(new GridLayout(10, 2, 0, 2));
		formPanel.add(new JCLabel("Vendor ID"));
		formPanel.add(vidCB);
		formPanel.add(new JCLabel("First Name"));
		formPanel.add(fNameTF);
		formPanel.add(new JCLabel("Last Name"));
		formPanel.add(lNameTF);
		formPanel.add(new JCLabel("Address"));
		formPanel.add(addressTF);
		formPanel.add(new JCLabel("Phone Number"));
		formPanel.add(phoneTF);
		formPanel.add(new JCLabel("Email"));
		formPanel.add(emailTF);
		formPanel.add(new JCLabel("UBI"));
		formPanel.add(ubiTF);
		formPanel.add(new JCLabel("Business name"));
		formPanel.add(bNameTF);
		formPanel.add(new JCLabel("Stall #"));
		formPanel.add(stallNumTF);
		formPanel.add(new JCLabel("Membership Paid?"));
		JPanel memPaidPanel = new JPanel(new GridLayout(1, 2));
		memPaidPanel.add(yesRB);
		memPaidPanel.add(noRB);
		formPanel.add(memPaidPanel);
		
		JPanel buttonPanel = new JPanel(new GridLayout());
		buttonPanel.add(new JPanel());
		buttonPanel.add(createButton("Update Vendor"));
		buttonPanel.add(new JPanel());
		
		setLayout(new BorderLayout());
		add(formPanel);
		add(buttonPanel, BorderLayout.SOUTH);
	}

	private void updateVendorInDB() {
		try {
			String yesNo = yesRB.isSelected() ? "YES" : "NO";
			db.updateVendor(vID, fNameTF.getText(),
					lNameTF.getText(), addressTF.getText(),
					phoneTF.getText(), emailTF.getText(), yesNo,
					ubiTF.getText(), bNameTF.getText(),
					stallNumTF.getText());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void populateFields() {
		String[] vendorData = new String[10];
		try {
			vendorData = db.viewVendor(vID);
			fNameTF.setText(vendorData[1]);
			lNameTF.setText(vendorData[2]);
			addressTF.setText(vendorData[3]);
			phoneTF.setText(vendorData[4]);
			emailTF.setText(vendorData[5]);
			ubiTF.setText(vendorData[6]);
			yesRB.setSelected(vendorData[7].equalsIgnoreCase("YES"));
			noRB.setSelected(vendorData[7].equalsIgnoreCase("NO"));
			bNameTF.setText(vendorData[8]);
			stallNumTF.setText(vendorData[9]);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void setVIDComboBox() {
		vidCB = new JComboBox();
		vidCB.setFont(custFont);
		vidCB.setModel(new DefaultComboBoxModel(getVIDStrings()));
		vidCB.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent the_event){
				JComboBox cbEvent = (JComboBox)the_event.getSource();
				vID = (String) cbEvent.getSelectedItem();
				populateFields();
			}
		});
		vidCB.setSelectedIndex(0);
		vID = (String) vidCB.getSelectedItem();
	}
	
	private String[] getVIDStrings() {
		int numAmount = 1;
		try {
			numAmount = db.getVIDs().size();
		} catch (ClassNotFoundException e) {
		} catch (SQLException e) {
		} catch (NullPointerException e) {}
		String[] numbers = new String[numAmount];
		for (int i = 0; i < numAmount; i++)
			numbers[i] = Integer.toString(i+1);
		return numbers;
	}
	
	public void refreshComboBox() {
		vidCB.setModel(new DefaultComboBoxModel(getVIDStrings()));
		vidCB.setSelectedIndex(Integer.parseInt(vID)-1);
	}

	private void setRadioButton(JRadioButton rb, String name) {
		rb.setFont(custFont);
		rb.setText(name);
		paidBG.add(rb);
	}

	private JButton createButton(final String name) {
		final JButton button = new JButton(name);
		button.setFont(custFont);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ae) {
				if (name.equalsIgnoreCase("Update Vendor")) {
					final int value = JOptionPane.showConfirmDialog(null,
							"Are you sure?", name, JOptionPane.OK_OPTION);
					if (value == JOptionPane.OK_OPTION) {
						if (isInteger(stallNumTF.getText())) {
							updateVendorInDB();
						} else {
							JOptionPane.showConfirmDialog(null, 
								"Input an Integer number for 'Stall #'", 
								"Error", JOptionPane.CLOSED_OPTION);
						}
					}
				}
			}
		});
		return button;
	}
	
	private boolean isInteger(String s) {
		try { Integer.parseInt(s); 
		} catch(NumberFormatException e) { return false; }
		return true;
	}
}
